﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinAPI;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace HookINCS
{
    public class MsgHook
    {
        private IntPtr hHook;
        public Win32API.HookProc hProc;
        public event MouseEventHandler Click;
        public MsgHook()
        {
            
        }
        public int SetHook()
        {
            hProc = new Win32API.HookProc(MouseHookProc);
            hHook = Win32API.SetWindowsHookEx(HookType.WH_FOREGROUNDIDLE, hProc, IntPtr.Zero, (uint)0);
            return hHook.ToInt32();
        }
        /// <summary>
        /// 写在钩子
        /// </summary>
        public void UnLock()
        {
            Win32API.UnhookWindowsHookEx(hHook);
        }
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return (int)Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                MsgHookStruct msgHookStruct = (MsgHookStruct)Marshal.PtrToStructure(lParam, typeof(MsgHookStruct));
                Console.WriteLine(msgHookStruct.message);
                switch (msgHookStruct.message)
                {
                    case (int)MsgType.WM_LBUTTONDOWN:
                        Console.WriteLine("鼠标左键");
                        break;
                    default:
                        break;
                }
                return (int)Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
        }
    }
}
